﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace jnxc.Web.nurseryRecord
{
    /// <summary>
    /// GetHz 的摘要说明
    /// </summary>
    public class GetHz : IHttpHandler, IRequiresSessionState
    {
        jnxc.BLL.nurseryRecord bll = new jnxc.BLL.nurseryRecord();
        StringBuilder result = new StringBuilder();

        StringBuilder sb = new StringBuilder();
        public void ProcessRequest(HttpContext context)
        {

            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }

            if (ac == "query")
                Query(context);  //林业有害生物普查         
            else if (ac == "tt")
                QueryFileTX(context);
            if (ac == "queryw")
                Queryw(context);//区外传入
            else if (ac == "ttw")
                QueryFileTXw(context);
            if (ac == "queryh")
                Queryh(context);//区外传入
            else if (ac == "tth")
                QueryFileTXh(context);
        }
        public void Queryh(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string StartTime = "", EndTime = "", otype = "0";
                string idstr = "";
                string areaid = "";
                int id = 0;
                int idh = 0;
                if (null != context.Request.Form["StartTime"])
                {//获取前台传来的值  
                    StartTime = context.Request.Form["StartTime"].ToString().Trim();
                }
                if (null != context.Request.Form["EndTime"])
                {//获取前台传来的值  
                    EndTime = context.Request.Form["EndTime"].ToString().Trim();
                }
                if (null != context.Request.Form["otype"])
                {//获取前台传来的值  
                    otype = context.Request.Form["otype"].ToString().Trim();
                }
                BLL.areainfo blla = new BLL.areainfo();
                DataSet ds1 = blla.GetList("");
                if (otype == "1")
                {
                    if (null != context.Request.Form["idh"])
                        idh = int.Parse(context.Request.Form["idh"]);
                }
                if (otype == "2")
                {
                    if (null != context.Request.Form["idstr"])
                    {
                        idstr = context.Request.Form["idstr"].ToString().Trim();
                        if (idstr != "")
                        {
                            id = int.Parse(idstr);
                            //根据点击ID找下面所有ID
                            result.Remove(0, result.Length);
                            areaid = id + GetIDByTable(ds1.Tables[0], "aid", "pid", id);
                        }
                    }

                }                
                else
                {
                    string[] pcc = Web.Admin.myinfo.getPCC(context).Split(',');
                    int level = Web.Admin.myinfo.getLevel(context);
                    int aid = blla.GetAid(level, pcc);
                    if (aid != 0)
                    {
                        result.Remove(0, result.Length);
                        areaid = aid + GetIDByTable(ds1.Tables[0], "aid", "pid", aid);

                    }
                    else
                        areaid = "";

                }
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (StartTime != "")
                {
                    strWhere.AppendFormat(" investigationTime>='{0}' and ", StartTime);
                }
                if (EndTime != "")
                {
                    strWhere.AppendFormat(" investigationTime<='{0}' and ", EndTime);
                }
                if (otype == "1")
                {
                    strWhere.AppendFormat(" mhid in ({0}) and ", idh);
                }
                if (areaid != "")
                {
                    strWhere.AppendFormat(" aid in ({0}) and ", areaid);
                }
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage7(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount7(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }
        public void Queryw(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string StartTime = "", EndTime = "";
                string idstr = "";
                string areaid = "";
                int id = 0;
                if (null != context.Request.Form["StartTime"])
                {//获取前台传来的值  
                    StartTime = context.Request.Form["StartTime"].ToString().Trim();
                }
                if (null != context.Request.Form["EndTime"])
                {//获取前台传来的值  
                    EndTime = context.Request.Form["EndTime"].ToString().Trim();
                }
                BLL.areainfo blla = new BLL.areainfo();
                DataSet ds1 = blla.GetList("");

                if (null != context.Request.Form["idstr"])
                {
                    idstr = context.Request.Form["idstr"].ToString().Trim();
                    if (idstr != "")
                    {
                        id = int.Parse(idstr);
                        //根据点击ID找下面所有ID
                        result.Remove(0, result.Length);
                        areaid = id + GetIDByTable(ds1.Tables[0], "aid", "pid", id);
                    }
                }
                else
                {
                    string[] pcc = Web.Admin.myinfo.getPCC(context).Split(',');
                    int level = Web.Admin.myinfo.getLevel(context);
                    int aid = blla.GetAid(level, pcc);
                    if (aid != 0)
                    {
                        result.Remove(0, result.Length);
                        areaid = aid + GetIDByTable(ds1.Tables[0], "aid", "pid", aid);

                    }
                    else
                        areaid = "";

                }
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (StartTime != "")
                {
                    strWhere.AppendFormat(" investigationTime>='{0}' and ", StartTime);
                }
                if (EndTime != "")
                {
                    strWhere.AppendFormat(" investigationTime<='{0}' and ", EndTime);
                }
                if (areaid != "")
                {
                    strWhere.AppendFormat(" aid in ({0}) and ", areaid);
                }
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage9(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount9(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }
        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string StartTime = "", EndTime = "";
                string idstr = "";
                string areaid = "";
                int id = 0;
                if (null != context.Request.Form["StartTime"])
                {//获取前台传来的值  
                    StartTime = context.Request.Form["StartTime"].ToString().Trim();
                }
                if (null != context.Request.Form["EndTime"])
                {//获取前台传来的值  
                    EndTime = context.Request.Form["EndTime"].ToString().Trim();
                }               
                BLL.areainfo blla = new BLL.areainfo();
                DataSet ds1 = blla.GetList("");

                if (null != context.Request.Form["idstr"])
                    {
                        idstr = context.Request.Form["idstr"].ToString().Trim();
                        if (idstr != "")
                        {
                            id = int.Parse(idstr);
                            //根据点击ID找下面所有ID
                            result.Remove(0, result.Length);
                            areaid = id + GetIDByTable(ds1.Tables[0], "aid", "pid", id);
                        }
                    }
                else
                {
                    string[] pcc = Web.Admin.myinfo.getPCC(context).Split(',');
                    int level = Web.Admin.myinfo.getLevel(context);
                    int aid = blla.GetAid(level, pcc);
                    if (aid != 0)
                    {
                        result.Remove(0, result.Length);
                        areaid = aid + GetIDByTable(ds1.Tables[0], "aid", "pid", aid);

                    }
                    else
                        areaid = "";

                } 
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (StartTime != "")
                {
                    strWhere.AppendFormat(" investigationTime>='{0}' and ", StartTime);
                }
                if (EndTime != "")
                {
                    strWhere.AppendFormat(" investigationTime<='{0}' and ", EndTime);
                }
                if (areaid != "")
                {
                    strWhere.AppendFormat(" aid in ({0}) and ", areaid);
                } 
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage7(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount7(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }
        private string GetIDByTable(DataTable tabel, string idCol, string rela, object pId)
        {
            result.Append(sb.ToString());
            sb.Remove(0, sb.Length);
            if (tabel.Rows.Count > 0)
            {
                string filer = string.Format("{0}='{1}'", rela, pId);
                DataRow[] rows = tabel.Select(filer);
                if (rows.Length > 0)
                {
                    foreach (DataRow row in rows)
                    {
                        sb.Append("," + row[idCol]);
                        if (tabel.Select(string.Format("{0}='{1}'", rela, row[idCol])).Length > 0)
                        {
                            GetIDByTable(tabel, idCol, rela, row[idCol]);
                            result.Append(sb.ToString());
                            sb.Remove(0, sb.Length);
                        }
                        result.Append(sb.ToString());
                        sb.Remove(0, sb.Length);
                    }
                }
                result.Append(sb.ToString());
                sb.Remove(0, sb.Length);
            }
            return result.ToString();
        }
        public void QueryFileTX(HttpContext context)
        {

            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string StartTime = "", EndTime = "",shr="",hzr="",hzt="",city="";
                string idstr = "";
                string areaid = "";
                int id = 0;
                string[] pcc = Web.Admin.myinfo.getPCC(context).Split(',');
                int level = Web.Admin.myinfo.getLevel(context);
                if (null != context.Request.QueryString["st"])
                {//获取前台传来的值  
                    StartTime = context.Request.QueryString["st"].ToString().Trim();
                }
                if (null != context.Request.QueryString["et"])
                {//获取前台传来的值  
                    EndTime = context.Request.QueryString["et"].ToString().Trim();
                }
                if (null != context.Request.QueryString["shr"])
                {//获取前台传来的值  
                    shr = context.Request.QueryString["shr"].ToString().Trim();
                }
                if (null != context.Request.QueryString["hzr"])
                {//获取前台传来的值  
                    hzr = context.Request.QueryString["hzr"].ToString().Trim();
                }
                if (null != context.Request.QueryString["hzt"])
                {//获取前台传来的值  
                    hzt = context.Request.QueryString["hzt"].ToString().Trim();
                }
                if (null != context.Request.QueryString["city"])
                {//获取前台传来的值  
                    city = context.Request.QueryString["city"].ToString().Trim();
                    if (city == "")
                    {
                        if (level > 3) city = "全国";
                        else if (level == 3) city = pcc[0];
                        else if (level == 2) city = pcc[1];
                        else if (level == 1) city = pcc[2];
                    }
                }
                BLL.areainfo blla = new BLL.areainfo();
                DataSet ds1 = blla.GetList("");

                if (null != context.Request.QueryString["id"])
                {
                    idstr = context.Request.QueryString["id"].ToString().Trim();
                    if (idstr != "")
                    {
                        id = int.Parse(idstr);
                        //根据点击ID找下面所有ID
                        result.Remove(0, result.Length);
                        areaid = id + GetIDByTable(ds1.Tables[0], "aid", "pid", id);
                    }
                }
                else
                {
                    
                    
                    int aid = blla.GetAid(level, pcc);
                    if (aid != 0)
                    {
                        result.Remove(0, result.Length);
                        areaid = aid + GetIDByTable(ds1.Tables[0], "aid", "pid", aid);

                    }
                    else
                        areaid = "";

                }                
                StringBuilder strWhere = new StringBuilder();
                if (StartTime != "")
                {
                    strWhere.AppendFormat(" investigationTime>='{0}' and ", StartTime);
                }
                if (EndTime != "")
                {
                    strWhere.AppendFormat(" investigationTime<='{0}' and ", EndTime);
                }
                if (areaid != "")
                {
                    strWhere.AppendFormat(" aid in ({0}) and ", areaid);
                }
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                DataSet ds = bll.GetListByPage7(strWhere.ToString(), "", 1, 100000);
                string tbh= DateTime.Now.Ticks.ToString();
                OutWord.outwordP(context.Server.MapPath("~/word/tpc.doc"), tbh + ".doc", context, ds,hzr,shr,hzt,city);
            }

        }

        public void QueryFileTXw(HttpContext context)
        {

            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string StartTime = "", EndTime = "", shr = "", hzr = "", hzt = "", city = "";
                string idstr = "";
                string areaid = "";
                int id = 0;
                string[] pcc = Web.Admin.myinfo.getPCC(context).Split(',');
                int level = Web.Admin.myinfo.getLevel(context);
                if (null != context.Request.QueryString["st"])
                {//获取前台传来的值  
                    StartTime = context.Request.QueryString["st"].ToString().Trim();
                }
                if (null != context.Request.QueryString["et"])
                {//获取前台传来的值  
                    EndTime = context.Request.QueryString["et"].ToString().Trim();
                }
                if (null != context.Request.QueryString["shr"])
                {//获取前台传来的值  
                    shr = context.Request.QueryString["shr"].ToString().Trim();
                }
                if (null != context.Request.QueryString["hzr"])
                {//获取前台传来的值  
                    hzr = context.Request.QueryString["hzr"].ToString().Trim();
                }
                if (null != context.Request.QueryString["hzt"])
                {//获取前台传来的值  
                    hzt = context.Request.QueryString["hzt"].ToString().Trim();
                }
                if (null != context.Request.QueryString["city"])
                {//获取前台传来的值  
                    city = context.Request.QueryString["city"].ToString().Trim();
                    if (city == "")
                    {
                        if (level > 3) city = "全国";
                        else if (level == 3) city = pcc[0];
                        else if (level == 2) city = pcc[1];
                        else if (level == 1) city = pcc[2];
                    }
                }
                BLL.areainfo blla = new BLL.areainfo();
                DataSet ds1 = blla.GetList("");

                if (null != context.Request.QueryString["id"])
                {
                    idstr = context.Request.QueryString["id"].ToString().Trim();
                    if (idstr != "")
                    {
                        id = int.Parse(idstr);
                        //根据点击ID找下面所有ID
                        result.Remove(0, result.Length);
                        areaid = id + GetIDByTable(ds1.Tables[0], "aid", "pid", id);
                    }
                }
                else
                {


                    int aid = blla.GetAid(level, pcc);
                    if (aid != 0)
                    {
                        result.Remove(0, result.Length);
                        areaid = aid + GetIDByTable(ds1.Tables[0], "aid", "pid", aid);

                    }
                    else
                        areaid = "";

                }
                StringBuilder strWhere = new StringBuilder();
                if (StartTime != "")
                {
                    strWhere.AppendFormat(" investigationTime>='{0}' and ", StartTime);
                }
                if (EndTime != "")
                {
                    strWhere.AppendFormat(" investigationTime<='{0}' and ", EndTime);
                }
                if (areaid != "")
                {
                    strWhere.AppendFormat(" aid in ({0}) and ", areaid);
                }
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                DataSet ds = bll.GetListByPage9(strWhere.ToString(), "", 1, 100000);
                string tbh = DateTime.Now.Ticks.ToString();
                OutWord.outwordP(context.Server.MapPath("~/word/tpw.doc"), tbh + ".doc", context, ds, hzr, shr, hzt, city);
            }

        }

        public void QueryFileTXh(HttpContext context)
        {

            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string StartTime = "", EndTime = "", shr = "", hzr = "", hzt = "", city = "",otype="0";
                string idstr = "";
                string areaid = "";
                int id = 0,idh=0;
                string[] pcc = Web.Admin.myinfo.getPCC(context).Split(',');
                int level = Web.Admin.myinfo.getLevel(context);
                if (null != context.Request.QueryString["st"])
                {//获取前台传来的值  
                    StartTime = context.Request.QueryString["st"].ToString().Trim();
                }
                if (null != context.Request.QueryString["et"])
                {//获取前台传来的值  
                    EndTime = context.Request.QueryString["et"].ToString().Trim();
                }
                if (null != context.Request.QueryString["shr"])
                {//获取前台传来的值  
                    shr = context.Request.QueryString["shr"].ToString().Trim();
                }
                if (null != context.Request.QueryString["hzr"])
                {//获取前台传来的值  
                    hzr = context.Request.QueryString["hzr"].ToString().Trim();
                }
                if (null != context.Request.QueryString["hzt"])
                {//获取前台传来的值  
                    hzt = context.Request.QueryString["hzt"].ToString().Trim();
                }
                if (null != context.Request.Form["otype"])
                {//获取前台传来的值  
                    otype = context.Request.Form["otype"].ToString().Trim();
                }
                if (null != context.Request.QueryString["city"])
                {//获取前台传来的值  
                    city = context.Request.QueryString["city"].ToString().Trim();
                    if (city == "")
                    {
                        if (level > 3) city = "全国";
                        else if (level == 3) city = pcc[0];
                        else if (level == 2) city = pcc[1];
                        else if (level == 1) city = pcc[2];
                    }
                }
                BLL.areainfo blla = new BLL.areainfo();
                DataSet ds1 = blla.GetList("");

                if (otype == "1")
                {
                    if (null != context.Request.Form["idh"])
                        idh = int.Parse(context.Request.Form["idh"]);
                }
                if (otype == "2")
                {
                    if (null != context.Request.Form["idstr"])
                    {
                        idstr = context.Request.Form["idstr"].ToString().Trim();
                        if (idstr != "")
                        {
                            id = int.Parse(idstr);
                            //根据点击ID找下面所有ID
                            result.Remove(0, result.Length);
                            areaid = id + GetIDByTable(ds1.Tables[0], "aid", "pid", id);
                        }
                    }

                }
                else
                {                    
                    int aid = blla.GetAid(level, pcc);
                    if (aid != 0)
                    {
                        result.Remove(0, result.Length);
                        areaid = aid + GetIDByTable(ds1.Tables[0], "aid", "pid", aid);
                    }
                    else
                        areaid = "";
                }
                StringBuilder strWhere = new StringBuilder();
                if (StartTime != "")
                {
                    strWhere.AppendFormat(" investigationTime>='{0}' and ", StartTime);
                }
                if (EndTime != "")
                {
                    strWhere.AppendFormat(" investigationTime<='{0}' and ", EndTime);
                }
                if (otype == "1")
                {
                    strWhere.AppendFormat(" mhid in ({0}) and ", idh);
                }
                if (areaid != "")
                {
                    strWhere.AppendFormat(" aid in ({0}) and ", areaid);
                }
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                DataSet ds = bll.GetListByPage7(strWhere.ToString(), "", 1, 100000);
                string tbh = DateTime.Now.Ticks.ToString();
                OutWord.outwordP(context.Server.MapPath("~/word/tpc.doc"), tbh + ".doc", context, ds, hzr, shr, hzt, city);
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}